
package com.jf.cloud.service.sysmanager.remote;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.taglib.Paging;
import com.jf.cloud.service.sysmanager.dao.IPubgClubService;
import com.jf.cloud.service.sysmanager.dao.IPubgExchangeOrderService;
import com.jf.cloud.service.sysmanager.dao.IPubgFinanceFlowDetailService;
import com.jf.cloud.service.sysmanager.dao.IPubgOperateLogService;
import com.jf.cloud.service.sysmanager.dao.IPubgUserService;
import com.jf.cloud.service.sysmanager.entity.PubgClub;
import com.jf.cloud.service.sysmanager.entity.PubgExchangeOrder;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * 商场物品兑换订单控制器
 * 
 * @author 代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/pubgExchangeOrder")
public class PubgExchangeOrderController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgExchangeOrderController.class);

	/**
	 * 商场物品兑换订单服务
	 */
	@Resource
	private IPubgExchangeOrderService pubgExchangeOrderService;
	/**
	 * 员工服务
	 */
	@Resource
	private IPubgUserService pubgUserService;

	/**
	 * 俱乐部服务
	 */
	@Resource
	private IPubgClubService pubgClubService;

	@Resource
	private IPubgFinanceFlowDetailService pubgFinanceFlowDetailService;

	@Resource
	private IPubgOperateLogService pubgOperateLogService;

	/**
	 * 后台商场物品兑换订单页面
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		try {
			List<PubgClub> clubList = pubgClubService.findList(new Query());
			request.setAttribute("clubList", clubList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsp/pubg/pubgExchangeOrder_list";
	}

	/**
	 * 后台获取商场物品兑换订单列表
	 *
	 * @author 高冬冬 2018年4月2日
	 */
	@ResponseBody
	@RequestMapping("/listPubgExchangeOrder.json")
	public AjaxPageData<PubgExchangeOrder> listPubgExchangeOrder(HttpServletRequest request, OPaging<PubgExchangeOrder> page) {
		page.setOrderBy(" ORDER BY APPLY_TIME DESC ");
		Paging<PubgExchangeOrder> paging = pubgExchangeOrderService.findPage(page);
		if (paging.getDataList() != null && paging.getDataList().size() > 0) {
			List<PubgExchangeOrder> newList = new ArrayList<PubgExchangeOrder>();
			for (PubgExchangeOrder pubgExchangeOrder:paging.getDataList()) {
				if (pubgExchangeOrder.getUserId() != null && pubgExchangeOrder.getUserId() > 0) {
					PubgUser pubgUser = pubgUserService.findByPK(pubgExchangeOrder.getUserId());
					pubgExchangeOrder.setPubgUser(pubgUser);
				}
				newList.add(pubgExchangeOrder);
			}
			paging.setDataList(newList);
		}
		return getAjaxPageData(paging);
	}

	/**
	 * 修改商场物品兑换订单状态
	 *
	 * @author 高冬冬 2018年4月4日
	 */
	@ResponseBody
	@RequestMapping("/updateOrderConversionState")
	public String updateOrderConversionState(HttpServletRequest request, Long rowId, String conversionState) {
		String result = "-2";
		try {
			if (rowId == null || conversionState == null) {
				return "2";
			}
			SysStaff user = getLoginUser(request);
			// 默认更新失败
			result = "2";
			if (user != null) {
				PubgExchangeOrder pubgExchangeOrder = pubgExchangeOrderService.findByPK(rowId);
				pubgExchangeOrderService.updateOrderConversionState(pubgExchangeOrder, conversionState, user.getRowId(), user.getStaffAccount(), "manage");
				return "1";
			}
		} catch (Exception e) {
			log.error("修改商场物品兑换订单状态出错了", e);
		}
		return result;
	}

}
